import time
from typing import TextIO

from bean.Constants import Constants
from bean.FieldInfo import FieldInfo
from utils.DateUtils import DateUtils


class CommentBuilder:
    @staticmethod
    def build_class_comment(file_pointer: TextIO, comment: str, is_add_search: bool = False) -> None:
        """
        生成注释包含日期与作者信息
        :param is_add_search:
        :param file_pointer: 要写入的文件指针
        :param comment: 注释
        :return:
        """
        file_pointer.write("\n")
        file_pointer.write("///此代码由代码生成器生成")
        if comment == "":
            file_pointer.write("\n")
            file_pointer.write(f"///\t&author:  {Constants.get_instance().PROJECT_AUTHOR}\n")
            file_pointer.write(f"///\t&date:  {DateUtils.format_date(Constants.get_instance().BEAN_DATE_FORMAT_FORM)}")
            return
        file_pointer.write("\n")
        file_pointer.write(f"///\t&author:  {Constants.get_instance().PROJECT_AUTHOR}\n")
        if not is_add_search:
            file_pointer.write(f"///\t&description:  {comment}\n")
        else:
            file_pointer.write(f"///\t&description:  {comment}查询\n")
        file_pointer.write(f"///\t&date:  {DateUtils.format_date(Constants.get_instance().BEAN_DATE_FORMAT_FORM)}")

    @staticmethod
    def build_comment_and_format(file_pointer: TextIO, filed_info: FieldInfo, is_skip_format: bool = False) -> None:
        if filed_info.comment == "":
            if is_skip_format:
                return
            if filed_info.filed_name in Constants.get_instance().IGNORE_BEAN_TOJSON_FILED:
                file_pointer.write("\t@JsonIgnore\n")
        else:
            file_pointer.write("\n")
            CommentBuilder.build_filed_comment(file_pointer, filed_info.comment, "\t")
            if is_skip_format:
                return
        if filed_info.filed_name in Constants.get_instance().IGNORE_BEAN_TOJSON_FILED:
            file_pointer.write("\t@JsonIgnore\n")
        if filed_info.java_type == "Date":
            file_pointer.write("\n")
            if filed_info.sql_type == "datetime":
                # 该死的bug让我愣了几秒每加value笑死
                time_form = Constants.get_instance().BEAN_DATE_FORMAT_FORM.value + " HH:mm:ss"
            else:
                time_form = Constants.get_instance().BEAN_DATE_FORMAT_FORM.value
            file_pointer.write(
                "\t" + Constants.get_instance().BEAN_DATE_FORMAT_EXPRESSION.format(
                    time_form
                )
            )
            file_pointer.write("\n")
            file_pointer.write(
                "\t" + Constants.get_instance().BEAN_DATE_UNFORMAT_EXPRESSION.format(
                    time_form
                )
            )

        file_pointer.write("\n")

    @staticmethod
    def build_filed_comment(file_pointer: TextIO, comment: str, retraction: str = "") -> None:
        """
        创建字段注释
        :param file_pointer:文件指针
        :param comment: 注释
        :param retraction: 缩进字符
        :return:
        """

        file_pointer.write(f"{retraction}/*\n")
        file_pointer.write(f"{retraction} * {comment}\n")
        file_pointer.write(f"{retraction} */")

    @staticmethod
    def build_xml_comment(file_pointer: TextIO, comment, retraction: str = "") -> None:
        """
        创建xml注释
        :param file_pointer:文件指针
        :param comment: 注释
        :param retraction: 缩进字符
        :return:
        """
        file_pointer.write(f"{retraction}<!--{comment}-->")

    @staticmethod
    def __date_time_format() -> str:
        current_time = time.localtime()
        return f"{current_time.tm_year}年{current_time.tm_mon}月{current_time.tm_mday}日"
